package com.leetcode.fenzhi;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * leetcode 973 最接近原点的k个点
 */
public class Solution973_2 {
    private static class Assist {
        private int index;

        private int distance;

        public Assist(int index, int distance) {
            this.index = index;
            this.distance = distance;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getDistance() {
            return distance;
        }

        public void setDistance(int distance) {
            this.distance = distance;
        }
    }

    public int[][] kClosest(int[][] points, int k) {
        if (points == null || points.length == 0) {
            return null;
        }


        PriorityQueue<Assist> queue = new PriorityQueue<>(new Comparator<Assist>() {
            @Override
            public int compare(Assist assist, Assist t1) {
                return assist.distance - t1.distance;
            }
        });
        for (int i = 0; i < points.length; i++) {
            queue.add(new Assist(i, (int)Math.pow(points[i][0], 2) + (int)Math.pow(points[i][1], 2)));
        }

        int[][] result = new int[k][2];
        for (int i = 0; i < k; i++) {
            result[i] = points[queue.poll().getIndex()];
        }

        return result;
    }
}
